home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / Stopwatch2.3 / Source / Session.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  129 lines

  1. /*
  2.  * For legal stuff see the file COPYRIGHT
  3.  */
  4. #import <sys/time.h>
  5. #import <stdio.h>
  6. #import <stdlib.h>
  7. #import "Session.h"
  8.  
  9. extern void freeAndCopy( char **ptr, const char *str );
  10. extern char *NXCopyStringBuffer(const char *buffer);
  11.  
  12. @implementation Session
  13.  
  14. - init:(const char *)startDate
  15.        time:(const char *)startTime
  16.        duration:(int)elapsedMinutes
  17.        description:(const char *)desc
  18. {
  19.   [self setStartDateString:startDate];
  20.   [self setStartTimeString:startTime];
  21.   [self setDescription:desc];
  22.   minutes = elapsedMinutes;
  23.   return self;
  24. }
  25.  
  26. - (void) freeDesc
  27. {
  28.   if ( description ) {
  29.     free( description );
  30.     description = NULL;
  31.   }
  32. }
  33.  
  34. - free
  35. {
  36.   [self freeDesc];
  37.   return [super free];
  38. }
  39.  
  40. - setMinutes:(int)value
  41. {
  42.   minutes = value;
  43.   return self;
  44. }
  45.  
  46. /* Must we parse the string and convert to UNIX format? Why bother? */
  47. - setStartDateString:(const char *)str
  48. {
  49.   freeAndCopy( &startDateString, str );
  50.   return self;
  51. }
  52.  
  53. - setStartTimeString:(const char *)str
  54. {
  55.   freeAndCopy( &startTimeString, str );
  56.   return self;
  57. }
  58.  
  59. - setDescription:(const char *)desc
  60. {
  61.   [self freeDesc];
  62.   description = NXCopyStringBuffer(desc);
  63.   return self;
  64. }
  65.  
  66. - (const char *)startTimeString
  67. {
  68.   return startTimeString;
  69. }
  70.  
  71. - (const char *)startDateString
  72. {
  73.   return startDateString;
  74. }
  75.  
  76. - (const char *)description
  77. {
  78.   return description;
  79. }
  80.  
  81. - (int)minutes
  82. {
  83.   return minutes;
  84. }
  85.  
  86. - (float)hours
  87. {
  88.   return minutes/60.0;
  89. }
  90.  
  91. /*
  92.  * For use in comparisons, convert date such as 11/21/93 to a
  93.  * single integer 932111. THIS SHOULD BE CACHED! (TBD)
  94.  */
  95. - (int)dateValue
  96. {
  97.   int day, month, year;
  98.  
  99.   sscanf( startDateString, "%d/%d/%d", &month, &day, &year );
  100.   return ( year * 10000 + month * 100 + day );
  101. }
  102.  
  103. /*
  104.  * Similarly, convert 12:45 to the integer 1245.
  105.  */
  106. - (int)timeValue
  107. {
  108.   int minute, hour;
  109.  
  110.   sscanf( startTimeString, "%d:%d", &hour, &minute );
  111.   return ( hour * 100 + minute );
  112. }
  113.  
  114. - read:(NXTypedStream *) stream
  115. {
  116.   NXReadTypes( stream, "***i", &description, &startTimeString,
  117.           &startDateString, &minutes );
  118.   return self;
  119. }
  120.  
  121. - write:(NXTypedStream *) stream
  122. {
  123.   NXWriteTypes( stream, "***i", &description, &startTimeString,
  124.            &startDateString, &minutes );
  125.   return self;
  126. }
  127.  
  128. @end
  129.